package com.gdcnano.libs.xspf4x
{
    import mx.collections.ArrayCollection;
    import mx.formatters.DateFormatter;
    
    
    /**
     * Class to work with XSPF playlists
     *
     * @see http://xspf.xiph.org/ for the detailed format description
     *
     * @author gareevm
     *
     */
    public class XSPF4X
    {
        
        private static var XSPF_XMLNS:String = "http://xspf.org/ns/0/";
        
        //TODO: defines which type will fit this properties better
        
        //private var _attribution:
        
        //private var _link:
        
        //private var _meta:
        
        //private var _extension:
        
        private static var _xspfns:Namespace = new Namespace(XSPF_XMLNS);
        
        private var _annotation:String;
        
        private var _creator:String;
        
        private var _date:Date;
        
        private var _identifier:String;
        
        private var _image:String;
        
        private var _info:String;
        
        private var _license:String;
        
        private var _title:String;
        
        [ArrayElementType("XSPFTrack")]
        private var _trackList:ArrayCollection;
        
        
        public function XSPF4X(xmlPlaylist:XML)
        {
            if (_xspfns != xmlPlaylist.namespace())
            {
                throw new ArgumentError("Playlist namespace is not correct. Please ensure, that xmlns=\"http://xspf.org/ns/0/\" is used.");
            }
            _title = xmlPlaylist._xspfns::title.toString();
            _annotation = xmlPlaylist._xspfns::annotation.toString();
            _creator = xmlPlaylist._xspfns::creator.toString();
            _info = xmlPlaylist._xspfns::info.toString();
            _identifier = xmlPlaylist._xspfns::identifier.toString();
            _image = xmlPlaylist._xspfns::image.toString();
            
            if (xmlPlaylist._xspfns::date.toString() != "")
            {
                // Commented because of using SDK 3.2 for FlexCoverage 
				// which doesn't supported DateFormatter.parseDateString 
				//_date = DateFormatter.parseDateString(xmlPlaylist._xspfns::date.toString());
            }
            _license = xmlPlaylist._xspfns::license.toString();
            
            _trackList = new ArrayCollection();
            
            for (var i:uint = 0; i < xmlPlaylist._xspfns::trackList._xspfns::track.length(); i++)
            {
                var xmlTrack:XML = xmlPlaylist._xspfns::trackList._xspfns::track[i];
                _trackList.addItem(xmlTracksToArray(xmlTrack));
            }
        }
        
        
        public static function get xspfNamespace():Namespace
        {
            return _xspfns;
        }
        
        
        public function get image():String
        {
            return _image;
        }
        
        
        public function get annotation():String
        {
            return _annotation;
        }
        
        
        public function get creator():String
        {
            return _creator;
        }
        
        
        public function get date():Date
        {
            return _date;
        }
        
        
        public function get identifier():String
        {
            return _identifier;
        }
        
        
        public function get info():String
        {
            return _info;
        }
        
        
        public function get license():String
        {
            return _license;
        }
        
        
        public function get title():String
        {
            return _title;
        }
        
        
        public function get trackList():ArrayCollection
        {
            return _trackList;
        }
        
        
        /**
         * Parses XML and creates collection of Track objects
         * @param trackList XML presentation of the tracklist
         * @return tracksArray collection of the Track objects
         *
         */
        private function xmlTracksToArray(xmlTrack:XML):XSPFTrack
        {
            var track:XSPFTrack = new XSPFTrack(xmlTrack);
            
            return track;
        }
    }
}